// 仅限查询功能,不要增删改
// use js native date type

var Global = (function(context) {
    var ChronoUnit = Java.type('java.time.temporal.ChronoUnit');
    var IsoFields = Java.type('java.time.temporal.IsoFields');
    var LocalDate = Java.type('java.time.LocalDate');

    // string to javascript Date
    function toDate(date) {
        if (date instanceof Date) {
            return date;
        } else {
            return new Date(date);
        }
    }

    // javascript Date to java LocalDate
    function toLocalDate(date) {
        return LocalDate.of(date.getFullYear(), date.getMonth() + 1, date.getDate());
    }

    // functions
    function convertConfig(config, options) {
        config = config ? config : {};
        if (config.startDate) {
            config.startDate = toLocalDate(toDate(config.startDate));
        }
        if (config.endDate) {
            config.endDate = toLocalDate(toDate(config.endDate));
        }

        for (var i = 0; i < options.length; i++) {
            if (!config[options[i]]) {
                config[options[i]] = null;
            }
        }

        return config
    }

    function journal(config) {
        config = convertConfig(config, ['startDate', 'endDate', 'filter']);
        return context.journal(config.startDate, config.endDate, config.filter);
    }

    function ledger(config) {
        config = convertConfig(config, ['startDate', 'endDate', 'filter', 'classifier', 'multiClassifier']);
        return context.ledger(config.startDate, config.endDate,
            config.filter, config.classifier, config.multiClassifier);
    }

    function balanceSheet(config) {
        config = convertConfig(config, ['startDate', 'endDate', 'period', 'ignoreZeroBalances']);
        return context.balanceSheet(config.startDate, config.endDate,
            config.period, config.ignoreZeroBalances);
    }

    function incomeStatement(config) {
        config = convertConfig(config, ['startDate', 'endDate', 'period', 'ignoreZeroBalances']);
        return context.incomeStatement(config.startDate, config.endDate,
            config.period, config.ignoreZeroBalances);
    }

    function cashFlow(config) {
        config = convertConfig(config, ['startDate', 'endDate', 'period', 'cashAccounts']);
        return context.cashFlow(config.startDate, config.endDate,
            config.period, config.cashAccounts);
    }

    function inventory(config) {
        config = convertConfig(config, ['startDate', 'endDate', 'commodities']);
        return context.inventory(config.startDate, config.endDate,
            config.commodities);
    }

    return {
        sugar: {
            // types
            Format: Java.type('daybook.io.formatter.Format'),
            LocalDate: Java.type('java.time.LocalDate'),
            // periods
            QuarterYears: IsoFields.QUARTER_YEARS,
            Days: ChronoUnit.DAYS,
            Weeks: ChronoUnit.WEEKS,
            Months: ChronoUnit.MONTHS,
            YEARS: ChronoUnit.YEARS,
            // converters
            toDate: toDate,
            toLocalDate: toLocalDate,
            // functions
            journal: journal,
            ledger: ledger,
            balanceSheet: balanceSheet,
            incomeStatement: incomeStatement,
            cashFlow: cashFlow,
            inventory: inventory
        },
        context: context
    }
})(context);

var B = Global.context;
var S = Global.sugar;

//var t = S.inventory({
//    startDate: "2016-06-08"
//});
//B.formatterFactory.get(S.Format.STRING).printInventory(t);
//print("******************");
//var t2 = S.inventory();
//B.formatterFactory.get(S.Format.STRING).printInventory(t2);

// Examples

//B.filterByDate("2016-06-01", "2016-06-03")
//    .map(function (tx) {
//        return tx.toPrettyString(B.book);
//    }).forEach(print);

//S.journal({
//    filter:
//    startDate:
//    endDate:
//})
//S.ledger({
//    filter: function (transactionEntry) {return true;} // Predicate<TransactionEntry>
//    classifier: byAccount
//    multiClassifier: byVirtualAccounts
//    startDate: xxx
//    endDate: yyy
//})
//S.balanceSheet({
//    startDate:
//    endDate:
//    period:
//    ignoreZeroBalances: true
//})
//S.incomeStatement({
//    startDate:
//    endDate:
//    period:
//    ignoreZeroBalances: true
//})
//S.cashFlow({
//    startDate:
//    endDate:
//    period:
//    cashAccounts:
//})
//S.Inventory({
//    startDate:
//    endDate:
//    commodities: function (commodity) {return false;} // Collection of commodities
//})